<?php
/**
 * Created by PhpStorm.
 * User: zhangchao
 * Date: 15/6/5
 * Time: 15:03
 */

namespace common\core;


use app\models\Users;
use yii\base\ActionFilter;
use yii\di\Instance;

class AccessControl extends ActionFilter{

    private $_user;

    public $is_debug = false;

    public $allowActions = [];


    public function getUser()
    {
        if(!$this->_user instanceof Users){
            $this->_user = Instance::ensure($this->_user,Users::className());
        }
        return $this->_user;
    }

    public function setUser($user)
    {
        $this->_user = $user;
    }

    public function beforeAction($action)
    {


        //如果是调试模式，就不进行rbac的验证
        if($this->is_debug){
            return true;
        }

//        if(\Yii::$app->user->isGuest){
//            \Yii::$app->controller->redirect('/site/login');
//        }
        $actionId = $action->getUniqueId();
        var_dump($actionId);
        var_dump($this->allowActions);die;
        var_dump(in_array($actionId,$this->allowActions));die;
        if(in_array($actionId,$this->allowActions)){

            return true;
        }
        var_dump(4444);die;

//        $user = $this->getUser();

//        if ($user->can('/' . $actionId)) {
//            return true;
//        }


//        $obj = $action->controller;
//        do {
//            if ($user->can('/' . ltrim($obj->getUniqueId() . '/*', '/'))) {
//                return true;
//            }
//            $obj = $obj->module;
//        } while ($obj !== null);
//        $this->denyAccess($user);
    }

}